import { useContext, useEffect, useState } from 'react';
import { DescriptionsType } from '@/components/DescriptionsType';
import { getLoadMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';
import { getAssetClassService } from '../services';
import { getCarUpdateConfig, getEquipmentFurnitureUpdateConfig, getOtherUpdateConfig } from '@/pages/AssetManage/MyAssetList/mconfig';
import { AcquisitionManageContext, IAcquisitionActionEnum } from '../mconfig';
import { Spin } from 'antd';
import ScrollBarLayout from '@/components/ScrollBarLayout';

const TableFormView: React.FC<any> = ({ formName, locationProps, currentType, changeLoading }: any) => {
  /** 样式根目录 */
  const styleFixCls = 'contract-styfixcls-';

  const [assetClassCode, setAssetClassCode] = useState<string>('');

  /** 当前材料类型 */
  const [selectAssetType, setSelectAssetType] = useState<string>('');

  const {
    // 表格数据及参数
    selectFormInfo, // 表单数据
    assetTypeInfo,
    acquisitionDispatch,
  } = useContext(AcquisitionManageContext);

  /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.AVERAGE_LIFE_METHOD}`, //折旧方法
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_MIDI}`, //计量单位
    `${getTenantID()}${ILoadMapTypeAll.ASSET_PURCHASE_TYPE}`, //采购方式
    `${getTenantID()}${ILoadMapTypeAll.ASSET_ACQUIRE_TYPE}`, //取得形式
    `${getTenantID()}${ILoadMapTypeAll.ASSET_VALUE_TYPE}`, //价值类型
    `${getTenantID()}${ILoadMapTypeAll.ASSET_CAR_TYPE}`, //车辆类型
    `${getTenantID()}${ILoadMapTypeAll.ASSET_USE_TYPE}`, //使用权类型
    `${getTenantID()}${ILoadMapTypeAll.MATERIEL_STATUS}`, //材料状态
    `${getTenantID()}${ILoadMapTypeAll.ASSET_STATUS}`, //资产状态
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`, // 资产分类
    `${getTenantID()}${ILoadMapTypeAll.LEDGER_ACCOUNT}`, // 单位会计科目
    `${getTenantID()}${ILoadMapTypeAll.ASSET_EQUITY}`, // 产权类型
    `${getTenantID()}${ILoadMapTypeAll.ASSET_LIFE_METHOD_STATUS}`, // 折旧/摊销状态
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`, // 品牌
    `${getTenantID()}${ILoadMapTypeAll.ASSET_AREA}`, // 存放区域
    `${getTenantID()}${ILoadMapTypeAll.ASSET_USED}`, // 资产用途
    `${getTenantID()}${ILoadMapTypeAll.ASSET_ORGANIZATION_STATUS}`, // 资产编制情况
    `${getTenantID()}${ILoadMapTypeAll.ASSET_ENTRY_STATE}`, // 财务入账状态
    `${getTenantID()}${ILoadMapTypeAll.ASSET_DENSE_STATE}`, // 定密状态
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`, // 品牌类型
    `${getTenantID()}${ILoadMapTypeAll.ASSET_VEHICLE_EXHAUST_VOLUME}`, // 车辆排气量
    `${getTenantID()}${ILoadMapTypeAll.ASSET_APPROVEDBY}`, // 配置批准单位
    `${getTenantID()}${ILoadMapTypeAll.ASSET_RESPONSIBILITY}`, // 使用责任主体
  ]);

  useEffect(() => {
    if (locationProps.record.assetClass) {
      changeLoading(true);
      getAssetClassRun(locationProps.record.assetClass);
    } else {
      setSelectAssetType('100124021002');
    }
  }, [locationProps]);

  /** 切换资产分类下当前表单类别 */
  const getAssetClassCallback = (res: Record<string, any>) => {
    if (res && res?.msgFlag && res?.obj) {
      setSelectAssetType(res?.obj);
      let obj = {
        assetType: res?.obj,
        assetClassCode,
      };
      acquisitionDispatch({
        type: IAcquisitionActionEnum.SELECT_ASSET_TYPE,
        payload: obj,
      });
    }
    changeLoading(false);
  };

  /** 获取资产分类下当前表单类别 */
  const { getAssetClassRun, getAssetClassLoading } = getAssetClassService(getAssetClassCallback);

  const changeAssetType = (value: string, node?: any) => {
    if (node) {
      setAssetClassCode(node?.shortCode);
    }
    changeLoading(true);
    /** 获取表单类型 */
    getAssetClassRun(value);
  };

  /** 获取表单配置
   *  @record   当前项
   *  @pageType  当前的页面类型
   *  @bothForm  共通表单实例
   */
  /** 土地 */
  // const { landConfig, landMethods } = getLandUpdateConfig({
  //   pageType: locationProps?.pageType ? locationProps?.pageType : '',
  //   record: locationProps?.record,
  //   assetLandForm: formName,
  //   mapLoading,
  //   loadMapInfo,
  //   changeAssetType,
  // });
  /** 房屋 */
  // const { houseConfig, houseMethods } = getHouseUpdateConfig({
  //   pageType: locationProps?.pageType ? locationProps?.pageType : '',
  //   record: locationProps?.record,
  //   assetHouseForm: formName,
  //   mapLoading,
  //   loadMapInfo,
  //   changeAssetType,
  // });
  /** 车辆 */
  const { carConfig, carMethods } = getCarUpdateConfig({
    pageType: locationProps?.pageType ? locationProps?.pageType : '',
    record: locationProps?.record,
    assetCarForm: formName,
    mapLoading,
    loadMapInfo,
    changeAssetType,
    selectAssetType,
  });
  /** 设备及家具 */
  const { equipmentFurnitureConfig, equipmentFurnitureMethods } = getEquipmentFurnitureUpdateConfig({
    pageType: locationProps?.pageType ? locationProps?.pageType : '',
    record: locationProps?.record,
    assetEquipmentFurnitureForm: formName,
    mapLoading,
    loadMapInfo,
    changeAssetType,
    selectAssetType,
  });
  /** 其他固定资产 */
  const { otherConfig, otherMethods } = getOtherUpdateConfig({
    pageType: locationProps?.pageType ? locationProps?.pageType : '',
    record: locationProps?.record,
    assetOtherForm: formName,
    mapLoading,
    loadMapInfo,
    changeAssetType,
    selectAssetType,
  });
  /** 专利及信息数据等无形资产 */
  // const { invisibleConfig, invisibleMethods } = getInvisibleUpdateConfig({
  //   pageType: locationProps?.pageType ? locationProps?.pageType : '',
  //   record: locationProps?.record,
  //   assetInvisibleForm: formName,
  //   mapLoading,
  //   loadMapInfo,
  //   changeAssetType,
  // });
  /** 在建工程 */
  // const { projectConfig, projectMethods } = getProjectUpdateConfig({
  //   pageType: locationProps?.pageType ? locationProps?.pageType : '',
  //   record: locationProps?.record,
  //   assetProjectForm: formName,
  //   mapLoading,
  //   loadMapInfo,
  //   changeAssetType,
  // });
  /** 租赁土地 */
  // const { leaseLandConfig, leaseLandMethods } = getLeaseLandUpdateConfig({
  //   pageType: locationProps?.pageType ? locationProps?.pageType : '',
  //   record: locationProps?.record,
  //   assetLeaseLandForm: formName,
  //   mapLoading,
  //   loadMapInfo,
  //   changeAssetType,
  // });
  /** 租赁房屋 */
  // const { leaseHouseConfig, leaseHouseMethods } = getLeaseHouseUpdateConfig({
  //   pageType: locationProps?.pageType ? locationProps?.pageType : '',
  //   record: locationProps?.record,
  //   assetLeaseHouseForm: formName,
  //   mapLoading,
  //   loadMapInfo,
  //   changeAssetType,
  // });
  /** 政府储备物资 */
  // const { stockpileConfig, stockpileMethods } = getStockpileUpdateConfig({
  //   pageType: locationProps?.pageType ? locationProps?.pageType : '',
  //   record: locationProps?.record,
  //   assetStockpileForm: formName,
  //   mapLoading,
  //   loadMapInfo,
  //   changeAssetType,
  // });
  /** 长期投资 */
  // const { investConfig, investMethods } = getInvestUpdateConfig({
  //   pageType: locationProps?.pageType ? locationProps?.pageType : '',
  //   record: locationProps?.record,
  //   assetInvestForm: formName,
  //   mapLoading,
  //   loadMapInfo,
  //   changeAssetType,
  // });

  // 表单map
  const optionsFormConfigs = {
    // '100124021000': landConfig,
    // '100124021001': houseConfig,
    '100124021002': carConfig,
    '100124021003': equipmentFurnitureConfig,
    '100124021004': otherConfig,
    // '100124021005': invisibleConfig,
    // '100124021006': projectConfig,
    // '100124021007': leaseLandConfig,
    // '100124021008': leaseHouseConfig,
    // '100124021009': stockpileConfig,
    // '100124021010': investConfig,
  };

  // useEffect(()=>{
  //   console.log("selectAssetType=====================selectAssetType",selectAssetType)
  // },[selectAssetType])

  return (
    <Spin spinning={mapLoading}>
      <ScrollBarLayout>
        {selectAssetType === '100124021002' ? <DescriptionsType config={optionsFormConfigs['100124021002']} /> : null}
        {selectAssetType === '100124021003' ? <DescriptionsType config={optionsFormConfigs['100124021003']} /> : null}
        {selectAssetType === '100124021004' ? <DescriptionsType config={optionsFormConfigs['100124021004']} /> : null}
        {/* <DescriptionsType config={optionsFormConfigs[selectAssetType]} /> */}
      </ScrollBarLayout>
    </Spin>
  );
};

export default TableFormView;
